package com.hss01248.dialog.material;

import com.hss01248.dialog.ResourceTable;
import com.hss01248.dialog.Tool;
import com.hss01248.dialog.adapter.SuperLvAdapter;
import com.hss01248.dialog.adapter.SuperLvHolder;
import com.hss01248.dialog.config.ChooseBean;
import com.hss01248.dialog.config.ConfigBean;
import com.hss01248.dialog.config.DefaultConfig;
import ohos.agp.components.*;
import ohos.agp.components.Text.TextSizeType;
import ohos.app.Context;

/**
 * Created by Administrator on 2017/11/19.
 */

public class MaterialDialogHolder extends SuperLvHolder<ConfigBean> {

    private DirectionalLayout contentView;
    private Text title;
    private ScrollView messageContentRoot;
    private DirectionalLayout messageContentView;
    private Text message;
    private Button btnP;
    private Button btnN;

    MdInputHolder mdInputHolder;

    public MaterialDialogHolder(Context context) {
        super(context);
    }

    @Override
    protected void findViews() {
        contentView = (DirectionalLayout) rootView.findComponentById(ResourceTable.Id_contentView);
        title = (Text) rootView.findComponentById(ResourceTable.Id_title);
        messageContentRoot = (ScrollView) rootView.findComponentById(ResourceTable.Id_message_content_root);
        messageContentView = (DirectionalLayout) rootView.findComponentById(ResourceTable.Id_message_content_view);
        message = (Text) rootView.findComponentById(ResourceTable.Id_message);
        btnP = (Button) rootView.findComponentById(ResourceTable.Id_btn_p);
        btnN = (Button) rootView.findComponentById(ResourceTable.Id_btn_n);
    }

    @Override
    protected int setLayoutRes() {
        return  ResourceTable.Layout_layout_material_dialog;
    }

    @Override
    public void assingDatasAndEvents(Context context, ConfigBean bean) {
        if(bean ==null){
            return;
        }

        setTitleStyle(bean);

        setContentStyle(context,bean);

        setBtnStyleAndEvent(context,bean);



    }

    private void setBtnStyleAndEvent(Context context, final ConfigBean bean) {
        if(Tool.isEmpty(bean.text2)){
            btnN.setVisibility(Component.HIDE);
        }else {
            btnN.setVisibility(Component.VISIBLE);
            btnN.setText(bean.text2.toString());
            Tool.setTextColorById(btnN, bean.btn2Color);
            btnN.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component v) {
                    Tool.dismiss(bean);
                    if(bean.listener!=null){
                        bean.listener.onSecond();
                    }
                }
            });
        }

        if(Tool.isEmpty(bean.text1)){
            btnP.setVisibility(Component.HIDE);
        }else {
            btnP.setVisibility(Component.VISIBLE);
            btnP.setText(bean.text1.toString());
            Tool.setTextColorById(btnP, bean.btn1Color);
            if(bean.type == DefaultConfig.TYPE_MD_MULTI_CHOOSE){
                return;
            }
            btnP.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component v) {
                    if(bean.type == DefaultConfig.TYPE_MD_INPUT){
                        boolean isvalid = bean.listener.onInputValid(mdInputHolder.getTxt1(),mdInputHolder.getTxt2(),mdInputHolder.getEt1(),mdInputHolder.getEt2());
                        if(!isvalid){
                            return;
                        }
                        bean.listener.onGetInput(mdInputHolder.getTxt1(),mdInputHolder.getTxt2());
                    }
                    Tool.dismiss(bean,true);
                    if(bean.listener!=null){
                        bean.listener.onFirst();
                    }
                }
            });
        }

    }

    private void setContentStyle(Context context, ConfigBean bean) {

        //input
        if(bean.type == DefaultConfig.TYPE_MD_INPUT){
            message.setVisibility(Component.HIDE);
            mdInputHolder = new MdInputHolder(context);
           //match parent
            DirectionalLayout.LayoutConfig params = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
            mdInputHolder.rootView.setLayoutConfig(params);
            messageContentView.addComponent(mdInputHolder.rootView);
            bean.needSoftKeyboard = true;
            mdInputHolder.assingDatasAndEvents(context,bean);
            return;
        }

        if(bean.type == DefaultConfig.TYPE_MD_SINGLE_CHOOSE){
            message.setVisibility(Component.HIDE);
            buildChoose(context,bean);
            return;
        }

        if(bean.type == DefaultConfig.TYPE_MD_MULTI_CHOOSE){
            message.setVisibility(Component.HIDE);
            buildChoose(context,bean);
            return;
        }

        //customview
        if(bean.customContentHolder !=null){
            message.setVisibility(Component.HIDE);
            messageContentView.addComponent(bean.customContentHolder.rootView);
            return;
        }

        //message
        if(Tool.isEmpty(bean.msg)){
            message.setVisibility(Component.HIDE);
        }else {
            message.setVisibility(Component.VISIBLE);
            message.setText(bean.msg.toString());
            message.setTextSize(bean.msgTxtSize, TextSizeType.FP);
            Tool.setTextColorById(message, bean.msgTxtColor);
        }

    }



    private void buildChoose(Context context, final ConfigBean bean) {

        messageContentRoot.setVisibility(Component.HIDE);
        final ListContainer listView = new ListContainer(context);
        SuperLvAdapter adapter = buildAdapterByType(bean);
        adapter.addAll(bean.chooseBeans);
        listView.setItemProvider(adapter);

        DirectionalLayout.LayoutConfig params = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        listView.setLayoutConfig(params);

        contentView.addComponent(listView,1);

        if(bean.type == DefaultConfig.TYPE_MD_SINGLE_CHOOSE){
            listView.setSelected(false);
            //todo 点击一个item即可消失
            listView.setItemClickedListener(new ListContainer.ItemClickedListener() {
                @Override
                public void onItemClicked(ListContainer parent, Component view, int position, long id) {
                    if(bean.listener !=null){
                        bean.listener.onGetChoose(position,bean.chooseBeans.get(position).txt);
                    }
                    if(bean.itemListener !=null){
                        bean.itemListener.onItemClick(bean.chooseBeans.get(position).txt,position);
                    }
                    Tool.dismiss(bean);
                }
            });
        }else {
            btnP.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component v) {
                }
            });
        }
        //初始化其选择状态
        for (int i = 0; i < bean.chooseBeans.size(); i++) {
            ChooseBean chooseBean = bean.chooseBeans.get(i);
        }
    }

    private SuperLvAdapter buildAdapterByType(final ConfigBean bean) {
        SuperLvAdapter adapter = null;
        if(bean.type == DefaultConfig.TYPE_MD_SINGLE_CHOOSE){
            adapter = new SuperLvAdapter(bean.context) {
                @Override
                protected SuperLvHolder generateNewHolder(Context context, int itemViewType) {
                    return new SingleChooseHolder(bean.context);
                }
            };
        }else  if(bean.type == DefaultConfig.TYPE_MD_MULTI_CHOOSE){
            adapter = new SuperLvAdapter(bean.context) {
                @Override
                protected SuperLvHolder generateNewHolder(Context context, int itemViewType) {
                    return new MultiChooseHolder(bean.context);
                }
            };
        }
        return adapter;
    }

    private void setTitleStyle(ConfigBean bean) {
        if(Tool.isEmpty(bean.title)){
            title.setVisibility(Component.HIDE);
        }else {
            title.setVisibility(Component.VISIBLE);
            title.setText(bean.title.toString());
            if(bean.titleTxtColor > 0){
                Tool.setTextColorById(title, bean.titleTxtColor);
            }
            if(bean.titleTxtSize>0){
                title.setTextSize(bean.titleTxtSize, TextSizeType.FP);
            }
        }

    }


}
